package com.jinyuanchun.learnjava.mapper.provider;

import com.jinyuanchun.learnjava.dao.PmsProject;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class PmsProjectProvider {
    public String existsInList(List<String> subs){
        String condition = String.join(",",subs);
        String sql = "select * from pms_project where subproject_id in ("+condition+")";
        return sql;
    }

    public String insertBatch(List<PmsProject> pmsProjects){
        String values = "";
        for(PmsProject item:pmsProjects){
            values += "('"+item.getSubprojectId()+"',";
            values += "'"+item.getProjectId()+"',";
            values += "'"+item.getAisType()+"',";
            values += "'"+item.getAisAnalysis()+"'),";
        }
        String value = values.replaceAll(",$","");
        String sql = "insert into pms_project (subproject_id,project_id,ais_type,ais_analysis) values "+value;
        return sql;
    }

    public String updateBatch(List<PmsProject> pmsProjects){
        String items = "";
        PmsProject elements = pmsProjects.get(0);
        if(elements.getSubprojectId() != null){items += "subproject_id,";}
        if(elements.getProjectId() != null){items += "project_id,";}
        if(elements.getAisType() != null){items += "ais_type,";}
        if(elements.getAisAnalysis() != null){items += "ais_analysis";}

        String values = "";
        for (PmsProject item:pmsProjects){
            if(elements.getSubprojectId() != null){values += "('"+elements.getSubprojectId()+"',";}
            if(elements.getProjectId() != null){items += "'"+elements.getProjectId()+"',";}
            if(elements.getAisType() != null){items += "'"+elements.getAisType()+"',";}
            if(elements.getAisAnalysis() != null){items += "'"+elements.getAisAnalysis()+"'),";}
        }
        String value = values.replaceAll(",$","");
        String sql = "insert into pms_project ("+items+") values "+value+" on duplicate key update " +
                "project_id=values(project_id)," +
                "ais_type=values(ais_type)," +
                "ais_analysis=values(ais_analysis)";

        return sql;
    }
}
